

import 'package:baselib/utils/load_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:baselib/widgets/comm_message_gradient_dialog.dart';
import 'package:baselib/widgets/refresh_util.dart';
import 'package:flutter/material.dart';
import 'package:framelib/base/base_view_model.dart';
import 'package:framelib/config/api_config.dart';
import 'package:framelib/net/baseentity.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:get/state_manager.dart';
import 'package:get/get.dart';
import 'package:module_business/module_comm/model/user_entity.dart';
import 'package:module_business/module_index/model/index_room_item_entity.dart';
import 'package:module_business/module_index/service/module_index_service.dart';
import 'package:module_business/module_my/service/module_my_service.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';


class MyFansViewModel extends BaseViewModel<ModuleMyService>{

  ModuleMyService moduleMyService;
  RefreshController refreshController = RefreshController(initialRefresh: true);
  RefreshState loadStatus=RefreshState.OK;
  int page = 1;
  bool isCanMoreData = false;
  List<UserEntity> dataLists = [];
  String title = '';
  String type = '';
  @override
  ModuleMyService createService(){
    return moduleMyService = ModuleMyService();
  }
  @override
  void onInit() {
    super.onInit();
    Map<String,dynamic> arguments = Get.arguments??{};
    type =  arguments['type'];
    title =  arguments['title'];
  }

  @override
  void onClose() {
    super.onClose();
  }
  onRefresh({int pageCur,}) async{
    if(pageCur!=null)page=pageCur;
    //type	类型(1：查看好友列表，2：查看粉丝列表，3：查看关注列表)
    Map<String,dynamic> upData={
      'currentPage': page,
      'pageSize': 10,
      "type":type,
    };
    BaseEntity baseEntity= await moduleMyService.api_user_userFriend(parmsMap: upData);
    if(refreshController!=null)refreshController.refreshCompleted();
    if(refreshController!=null)refreshController.loadComplete();
    if(baseEntity?.code==ApiConfig.SUCCESS_CODE){
      List<UserEntity> dataTemp=baseEntity?.result??[];
      if(page==1){
        dataLists=dataTemp;
      }else{
        dataLists.addAll(dataTemp);
      }
      if(((dataTemp?.length??0)<10)){
        isCanMoreData=false;
      }else{
        isCanMoreData=true;
      }
      //计算是否为空
      if(((dataLists?.length??0)==0)){
        loadStatus=RefreshState.EMPTY;
      }else{
        //计算被选中的
        loadStatus=RefreshState.OK;
      }
    }else{
      loadStatus=RefreshState.NET_ERROR;
      ToastUtil.toast(baseEntity?.message);
    }
    update();
  }

  followOper(UserEntity dataItem) async{
    LoadUtil.showLoad(loadingText: '${StringUtil.instance.submiting}...');
    Map<String,dynamic> upData={
      "followUserId": dataItem?.userId
    };
    //动作类型（1：关注，2：取消关注，3：拉黑用户）
    if(dataItem?.followed??false){
      upData['followType']=2;
    }else{
      upData['followType']=1;
    }
    BaseEntity baseEntity = await moduleMyService.api_user_followUser(parmsMap: upData);
    LoadUtil.hideLoad();
    if(baseEntity?.code == ApiConfig.SUCCESS_CODE){
      dataItem?.followed = !(dataItem?.followed??false);
      update();
    }else{
      ToastUtil.toast(baseEntity?.message);
    }
  }
}
